#!/usr/bin/perl
use Time::HiRes 'time';
require 'forum_connect.pl';
require 'forum_html.pl';

$dbh = connect_to_database() or bad_deal();
$user = get_current_user(get_sess_id());

print(redirect(-location=>$forum::root_path)),exit unless($user->{PRIV}eq'A');



$default_query = 'SHOW TABLE STATUS';

$query = ($query = param('query')) ? $query : $default_query;

print	header('text/html'),
		start_html(-style=>{-src=>'tables.css'},
				   -title=>"Database: $forum::db"),
		h1("Database: $forum::db"), p("You are logged in as $user->{USERNM}");

$start_time = time;
$sth = $dbh->prepare($query);
unless ($sth && $sth->execute())
{
	print forum_error($dbh->errstr());
	$start_time = time;
	$sth = $dbh->prepare($default_query);
	$sth->execute();
}

$total_time = time() - $start_time;

print	'<div>',
		start_form(-action=>'', -method=>'get'),
		textarea(-name=>'query',-cols=>50,-rows=>5,-value=>$query,-override=>1),
		br(),
		submit('submit', 'New query'),
		end_form(),
		'</div>';

print h2('Rows: ', $sth->rows()), h3("Time: $total_time seconds"), '<table>';

print '<tr>';
print th($_) for (@{$sth->{NAME}});
print '</tr>';
while (@row = $sth->fetchrow())
{
	print '<tr>';
	print td(escapeHTML($_)) for (@row);
	print '</tr>';
}

print '</table>', end_html(), "\n";
